@using BlazorApp.Utils.Prometheus.Models.Interfaces
@using Extension
@using k8s
@inherits BlazorApp.Pages.Common.PageBase
@if (AllMetrics is { Count: > 0 })
{
    if (AllMetrics.Any(x => x.Name == MetricName))
    {
        var groups = AllMetrics.FirstOrDefault(x => x.Name == MetricName)?
            .Measurements?
            .Where(x => x.Name == $"{MetricName}_count")
            .Select(x => x.Labels.Where(lb => MultipleGroupBy.Contains(lb.Key))
                .ToDictionary(kv => kv.Key, kv => kv.Value)
            )
            .Distinct()
            .ToList();

//去重
        IDictionary<string, Dictionary<string, string>> distinctList = new Dictionary<string, Dictionary<string, string>>();
        if (groups != null)
        {
            foreach (var enumerable in groups)
            {
                var hash = KubernetesJson.Serialize(enumerable).ToMd5Str();
                if (!distinctList.ContainsKey(hash))
                {
                    distinctList.Add(hash, enumerable);
                }
            }
        }

        var group = distinctList.Values;
        var keys = group.SelectMany(x => x.Keys).Distinct().ToList();
        <GridRow Class="grid-table">
            <GridCol Span="6">@($"{L["Item"]}({string.Join(",", keys)})")</GridCol>
            <GridCol Span="6">@($"{L["PercentileChart"]}(99,90,50)")</GridCol>
            <GridCol Span="4">@("P99")</GridCol>
            <GridCol Span="4">@("P95")</GridCol>
            <GridCol Span="4">@("P50")</GridCol>
        </GridRow>
        foreach (var item in group)
        {
            var allCountLinq = AllMetrics
                .FirstOrDefault(x => x.Name == MetricName)?
                .Measurements
                .Where(x => x.Name == $"{MetricName}_count");
            foreach (var (k, v) in item)
            {
                allCountLinq = allCountLinq?.Where(x => x.Labels[k] == v);
            }

            var allCount = allCountLinq.Where(x => x.Labels.Keys.Any(y => MultipleGroupBy.Contains(y)))
                .Sum(x => x.Value);

            var leKeyValueTupleListLinq = AllMetrics
                .FirstOrDefault(x => x.Name == MetricName)?
                .Measurements
                .Where(x => x.Name == $"{MetricName}_bucket")
                .Where(x => !x.Labels["le"].ToLower().Contains("inf"));
            foreach (var (k, v) in item)
            {
                leKeyValueTupleListLinq = leKeyValueTupleListLinq?.Where(x => x.Labels[k] == v);
            }

            var leKeyValueTupleList = leKeyValueTupleListLinq
                .Select(x => (double.Parse(x.Labels["le"]), x.Value));


            double p99Count = 0;
            double p95Count = 0;
            double p90Count = 0;
            double p75Count = 0;
            double p50Count = 0;
            double p25Count = 0;
            double p99Time = 0;
            double p95Time = 0;
            double p90Time = 0;
            double p75Time = 0;
            double p50Time = 0;
            double p25Time = 0;
//t代表时间，v代表count个数
            foreach (var (t, v) in leKeyValueTupleList!)
            {
                var ret = v / allCount * 100;
                @* @($"{group}-{t}={v};allCount{allCount};ret={ret}")<br> *@

                switch (ret)
                {
                    case >= 99:
                    {
                        if (p99Count == 0)
                        {
                            p99Time = t;
                            p99Count = v;
                        }

                        break;
                    }
                    case >= 95 and < 99:
                        if (p95Count == 0)
                        {
                            p95Time = t;
                            p95Count = v;
                        }

                        break;
                    case >= 90 and < 95:
                        if (p90Count == 0)
                        {
                            p90Time = t;
                            p90Count = v;
                        }

                        break;
                    case >= 75 and < 90:
                        if (p75Count == 0)
                        {
                            p75Time = t;
                            p75Count = v;
                        }

                        break;
                    case >= 50 and < 70:
                        if (p50Count == 0)
                        {
                            p50Time = t;
                            p50Count = v;
                        }

                        break;
                    case >= 25 and < 50:
                        if (p25Count == 0)
                        {
                            p25Time = t;
                            p25Count = v;
                        }

                        break;
                }
            }


            <GridRow Class="grid-table">
                @{
                    var vv = item.Values.Where(x => !x.IsNullOrEmpty()).ToList();
                    var rvv = string.Join("/", vv);
                }
                <GridCol Span="6">@($"{rvv}")</GridCol>
                <GridCol Span="6">
                    <MetricsHistogramPercentChatView
                        Data="@(new List<double> { p99Count, p95Count, p50Count })">
                    </MetricsHistogramPercentChatView>
                </GridCol>
                <GridCol Span="4">
                    <NumberFormat Unit="@Unit" Number="@p99Time"></NumberFormat>
                    <div style="font-size: 6pt">@($"{p99Count}")</div>
                </GridCol>
                <GridCol Span="4">
                    <NumberFormat Unit="@Unit" Number="@p95Time"></NumberFormat>
                    <div style="font-size: 6pt">@($"{p95Count}")</div>
                </GridCol>
                <GridCol Span="4">
                    <NumberFormat Unit="@Unit" Number="@p50Time"></NumberFormat>
                    <div style="font-size: 6pt">@($"{p50Count}")</div>
                </GridCol>
            </GridRow>
        }
    }
}

@code {
    [Parameter] public IList<IMetric> AllMetrics { get; set; }

    [Parameter] public string MetricName { get; set; }

    [Parameter] public string Title { get; set; }
    [Parameter] public List<string> MultipleGroupBy { get; set; }
    [Parameter] public string Unit { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
    }

}
